import _ from 'lodash';

import Session from '@core/engine/Session';
import { OrderParams, OrderResourceParams } from '@core/engine/Order';
import * as Enums from '@firewall/engine/Enums';

export interface OrderDynamicParams {
  cycleCount?: number,
  cycleType?: number,
  masterConfig?: Record<string, any>,
  protectionIpCount?: number,
  flowProcessingCapacity?: number;
  resourceIdTypeMap?: Record<string, string>,
}

export default class OrderHelper {
  static MIN_IP_COUNT = Enums.ProtectionIpCountSteps[0];
  static MAX_IP_COUNT = _.last(Enums.ProtectionIpCountSteps);
  static MIN_FLOW_CAPACITY = Enums.FlowCapacitySteps[0];
  static MAX_FLOW_CAPACITY = _.last(Enums.FlowCapacitySteps);
  static FLOW_CAPACITY_STEP = 5;

  static buildOrderParams = (params: OrderDynamicParams): OrderParams | null => {
    const regionId = Session.getRegionId();
    if (!regionId) {
      return null;
    }
    const { protectionIpCount, flowProcessingCapacity } = params;
    if (!protectionIpCount || !flowProcessingCapacity) {
      return null;
    }

    const resourcesParams: Array<OrderResourceParams> = [];
    const { vpcId, vpcIp, firewallName, firewallEdition } = params.masterConfig || {};
    resourcesParams.push({
      master: true,
      resourceType: Enums.OrderResourceType.Master,
      serviceTag: Enums.ORDER_SERVICE_TAG,
      itemConfig: {
        regionId, vpcId, vpcIp, firewallName, firewallEdition,
      },
      itemValue: 1,
      resourceId: params.resourceIdTypeMap?.[Enums.OrderResourceType.Master],
    });

    const appendIpCount = protectionIpCount - OrderHelper.MIN_IP_COUNT;
    if (appendIpCount > 0) {
      resourcesParams.push({
        master: false,
        resourceType: Enums.OrderResourceType.Ip,
        serviceTag: Enums.ORDER_SERVICE_TAG,
        itemConfig: {
          protectionIpNum: appendIpCount,
        },
        itemValue: appendIpCount,
        resourceId: params.resourceIdTypeMap?.[Enums.OrderResourceType.Ip],
      });
    }

    const appendFlowCapacity = flowProcessingCapacity - OrderHelper.MIN_FLOW_CAPACITY;
    if (appendFlowCapacity > 0) {
      resourcesParams.push({
        master: false,
        resourceType: Enums.OrderResourceType.Bandwidth,
        serviceTag: Enums.ORDER_SERVICE_TAG,
        itemConfig: {
          flowProcessingCapacity: appendFlowCapacity,
        },
        itemValue: appendFlowCapacity,
        resourceId: params.resourceIdTypeMap?.[Enums.OrderResourceType.Bandwidth],
      });
    }

    return {
      instanceCnt: 1,
      cycleCnt: params.cycleCount,
      cycleType: params.cycleType,
      items: resourcesParams,
    };
  };

  static getCycleConfig = (sliderCount: number) => {
    let count = 1;
    let type = Enums.OrderCycleType.Month;
    if (sliderCount < Enums.MONTHS_PRE_YEAR) {
      count = sliderCount;
      type = Enums.OrderCycleType.Month;
    } else {
      switch (sliderCount) {
        case 12: type = Enums.OrderCycleType.Year; break;
        case 13: type = Enums.OrderCycleType.DoubleYears; break;
        case 14: type = Enums.OrderCycleType.TripleYears; break;
        default: break;
      }
      count = 1;
    }
    return { type, count };
  };
}
